using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using System.Net;
using System.Windows.Forms;

namespace Client
{
    class Login
    {
        

        public static bool login(Message msg)
        {
            NetworkStream output; // stream for receiving data           
            BinaryWriter writer; // facilitates writing to the stream    
            BinaryReader reader; // facilitates reading from the stream  
            Thread readThread; // Thread for processing incoming messages
            string message = "";
            bool flag = false;
            TcpClient client;
            // instantiate TcpClient for sending data to server
            try
            {
                // Step 1: create TcpClient and connect to server                
                client = new TcpClient();
                client.Connect("10.15.83.90", 1917);

                // Step 2: get NetworkStream associated with TcpClient
                output = client.GetStream();

                // create objects for writing and reading across stream
                writer = new BinaryWriter(output);
                reader = new BinaryReader(output);
                //Send message to authenticate!
                writer.Write(msg.Content);
                //Receive response!
                do
                {
                    // Step 3: processing phase
                    try
                    {
                        // read message from server                                              
                        message = reader.ReadString();
                        Message mess = new Message(message);                        
                        if (Int32.Parse(mess.ResponseCode) == OTPDB_DEFINE.DB_ERR_SUCCESS)
                        {
                            //MessageBox.Show("Login successful!!!!!   " + message + "     Response code is : " + mess.ResponseCode);                            
                            flag = true;
                            break;
                        }
                        else
                        {
                            //MessageBox.Show("Login fault!!!!    " + message + "     Response code is : " + mess.ResponseCode);
                            flag = false;
                            break;
                        }
                    } // end try
                    catch (Exception e)
                    {
                        //MessageBox.Show("You try to connect again!!");
                        flag = false;
                        break;
                    } // end catch
                } while (client.Connected);

                // Step 4: close connection
                writer.Close();
                reader.Close();
                output.Close();
                client.Close();
            } // end try
            catch (Exception error)
            {
                return false;
                // handle exception if error in establishing connection
                MessageBox.Show(error.ToString(), "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                System.Environment.Exit(System.Environment.ExitCode);
            } // end catch

            return flag;
        }
    }
}
